package objects;

import java.text.SimpleDateFormat;
import java.util.*;

public class Event implements Comparable<Event> {
	private final int NO_PRIORITY = -1;
	private final int INFINITE_YEAR = 2999;
	private final int LONG_BEFORE_YEAR = 1900;
	private final Calendar NO_ENDDATE = this.getFarAwayDate();
	private final Calendar NO_STARTDATE = this.getBeforeDate();
	private int Id;
	private String DETAILS;
	private String TAG;
	private int PRIORITY;
	private Calendar STARTDATE;
	private Calendar ENDDATE;
	private ArrayList<Event> SUBTASKS;

	public Event() {
		Id = IdGenerator();
		DETAILS = new String();
		TAG = new String();
		PRIORITY = NO_PRIORITY;
		STARTDATE = NO_STARTDATE;
		ENDDATE = NO_ENDDATE;
		SUBTASKS = new ArrayList<Event>();
	}

	public Event(String PARAM_DETAILS, String PARAM_TAG, int PARAM_PRIORITY,
			Calendar PARAM_STARTDATE, Calendar PARAM_ENDDATE,
			ArrayList<Event> PARAM_SUBTASK) {
		Id = IdGenerator();
		DETAILS = PARAM_DETAILS;
		TAG = PARAM_TAG.toLowerCase();
		PRIORITY = PARAM_PRIORITY;
		STARTDATE = PARAM_STARTDATE;
		ENDDATE = PARAM_ENDDATE;
		SUBTASKS = PARAM_SUBTASK;
	}

	public boolean equals(Object obj) {
		if (obj instanceof Event) {
			Event x = (Event) obj;
			if (Id == x.Id) {
				return true;
			} else {
				return false;
			}
		}
		return super.equals(obj);
	}

	public int compareTo(Event x) {
		// TODO Auto-generated method stub
		if (this.equals(x)) {
			return 0;
		} else if (STARTDATE != null) {
			return STARTDATE.compareTo(x.getEndDate());
		} else if (x.STARTDATE != null) {
			return -1;
		} else {
			return 0;
		}
	}

	public boolean containsString(Event x) {
		if (DETAILS.contains(x.getDetails())) {
			return true;
		} else
			return false;
	}

	public String toString() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
		String startDateString = df.format(STARTDATE.getTime());
		String endDateString = df.format(ENDDATE.getTime());
		String priorityString = Integer.toString(PRIORITY);
		String tagString = TAG;
		ArrayList<String> subTaskStrings = new ArrayList<String>();
		if (SUBTASKS.size() != 0) {
			for (int i = 0; i < SUBTASKS.size(); i++) {
				subTaskStrings.add(SUBTASKS.get(i).toString());
			}
			String subTasksString = new String();
			for (int i = 0; i < subTaskStrings.size(); i++) {
				subTasksString = "\n    " + subTasksString + subTaskStrings.get(i);
			}
		}

		if (STARTDATE.equals(NO_STARTDATE)) {
			startDateString = "EMPTY";
		}
		if (ENDDATE.equals(NO_ENDDATE)) {
			endDateString = "EMPTY";
		}
		if (PRIORITY == NO_PRIORITY) {
			priorityString = "EMPTY";
		}
		if (TAG .equals("")) {
			tagString = "EMPTY";
		}
		String s = Id + " " + DETAILS + " " + startDateString + " - "
				+ endDateString + " " + " tag: " + tagString + " "
				+ " priority: " + priorityString + " ";
          
		return s;
	}

	public String getDetails() {
		return DETAILS;
	}

	public String getTag() {
		return TAG;
	}

	public int getPriority() {
		return PRIORITY;
	}

	public Calendar getStartDate() {
		return STARTDATE;
	}

	public Calendar getEndDate() {
		return ENDDATE;
	}

	public ArrayList<Event> getSubTasks() {
		return SUBTASKS;
	}

	public void setSubTask(ArrayList<Event> subtasks) {
		this.SUBTASKS = subtasks;
	}

	public void setStartDate(Calendar d) {
		STARTDATE = d;
	}

	public void setEndDate(Calendar d) {
		ENDDATE = d;
	}

	public void setDetails(String s) {
		DETAILS = s;
	}

	public void setTag(String t) {
		TAG = t;
	}

	public void setId(int id) {
		this.Id = id;
	}

	public void setPriority(int b) {
		PRIORITY = b;
	}

	public int getId() {
		return Id;
	}

	private Calendar getBeforeDate() {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(LONG_BEFORE_YEAR, 1, 1);// Far Away Date
		return cal;
	}

	private Calendar getFarAwayDate() {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(INFINITE_YEAR, 12, 31);// Far Away Date
		return cal;
	}

	private int IdGenerator() {
		Calendar cal = Calendar.getInstance();
		int y = cal.getTime().hashCode();
		return y;
	}

}
